//
//  ChangePriceController.m
//  YiBaoStore
//
//  Created by 毛云河 on 2018/12/1.
//  Copyright © 2018 小河. All rights reserved.
//

#import "ChangePriceController.h"

@interface ChangePriceController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSMutableArray *tableViewArray;
@property (nonatomic, copy) NSString *nextPageInfo;
@property (nonatomic, assign) int changeCount;


@end

@implementation ChangePriceController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.tableViewArray = [NSMutableArray array];
    [self createTableView];
    [self httpRequestData];
}

#pragma mark - 请求数据
- (void)httpRequestData
{
    __weak ChangePriceController *myself = self;
    [HttpRequest formPostPath:CHANGEPRICEGOODS_API params:nil resultBlock:^(NSDictionary *Data, MsgModel *msgModel)
    {
//        NSLog(@"已改价Data-%@",Data);
        if (msgModel.status == 200)
        {
            NSDictionary *result = Data[@"result"];
            NSLog(@"已改价result-%@",result);
            myself.changeCount = msgModel.changeCount;
            NSArray *currentPageResult = (NSArray *)result[@"currentPageResult"];
            NSError *error;
            
            for (NSDictionary *dic in currentPageResult)
            {
                MainModel *model = [[MainModel alloc] initWithDictionary:dic error:&error];
                [myself.tableViewArray addObject:model];
            }
            [myself.myTableView.mj_header endRefreshing];
            myself.myTableView.tableHeaderView = [self tableViewHeader];
            [myself.myTableView reloadData];
        }else {
            [myself.myTableView.mj_header endRefreshing];
        }
    }];

}

- (void)httpRequestNewData
{
    self.tableViewArray = [NSMutableArray array];
    [self httpRequestData];
}

- (void)createTableView
{
    if (@available(iOS 11.0, *))
    {
        self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT-46-40-TopBarHeight_xs-BottomHeight_xs-TopBarHeight) style:UITableViewStyleGrouped];
    }
    else {
        self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT-46-40-TopBarHeight-BottomHeight-TopBarHeight) style:UITableViewStyleGrouped];
    }
    self.myTableView.backgroundColor = RGBACOLOR(241, 241, 241, 1);
    [self.view addSubview:self.myTableView];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.myTableView.sectionHeaderHeight = 10.f;
    self.myTableView.sectionFooterHeight = 0.01f;
    self.myTableView.tableFooterView = [self tableViewFoot];
    self.myTableView.tableHeaderView = [self tableViewHeader];
    [TTKUtil setAutomaticallyAdjustsScrollViewInsetsWidth:self.myTableView withView:self];
    [self.myTableView registerClass:[ChangePriceCell class] forCellReuseIdentifier:@"ChangePriceCell"];
    
    self.myTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(httpRequestNewData)];
//    self.myTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(httpMoreData)];
}

- (UIView *)tableViewHeader
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, self.changeCount>0?40:0.01f)];
    
    UILabel *titlLabel = [[UILabel alloc] init];
    titlLabel.hidden = self.changeCount>0?NO:YES;
    [view addSubview:titlLabel];
    titlLabel.frame = CGRectMake(15, 0, SCREENWIDTH-30, 40);
    titlLabel.font = FontWithSize(12);
    titlLabel.textAlignment = 0;
    titlLabel.textColor = RGBACOLOR(153, 153, 153, 1);
    titlLabel.text = [NSString stringWithFormat:@"您有%d个商品成本价格发生变化，请查看",self.changeCount];
    
    return view;
}

- (UIView *)tableViewFoot
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 0.01)];
    return view;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    [self.myTableView tableViewDisplayWitMsg:@"暂无已推荐商品" ifNecessaryForRowCount:self.tableViewArray.count withBottonTitle:@"查看全部商品"];
    __weak ChangePriceController *myself = self;
    self.myTableView.myBlcok = ^(int index)
    {
        AllGoodsController *allVC = [[AllGoodsController alloc]init];
        allVC.hidesBottomBarWhenPushed = YES;
        [myself.navigationController pushViewController:allVC animated:YES];
    };
    return self.tableViewArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 420.f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ChangePriceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChangePriceCell"];
    cell.shanrkBtn.indexPath = indexPath;
    cell.changePriceBtn.indexPath = indexPath;
    [cell.shanrkBtn addTarget:self action:@selector(sharkShoppingClick:) forControlEvents:UIControlEventTouchUpInside];
    [cell.changePriceBtn addTarget:self action:@selector(changePriceBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    cell.topTitleLabelBtn.indexPath = indexPath;
    cell.leftImageBtn.indexPath = indexPath;
    cell.rightImgaeBtn.indexPath = indexPath;
    
    [cell.topTitleLabelBtn addTarget:self action:@selector(gotoGoodsDelitgClick:) forControlEvents:UIControlEventTouchUpInside];
    [cell.leftImageBtn addTarget:self action:@selector(gotoGoodsDelitgClick:) forControlEvents:UIControlEventTouchUpInside];
    [cell.rightImgaeBtn addTarget:self action:@selector(gotoGoodsDelitgClick:) forControlEvents:UIControlEventTouchUpInside];
    
    MainModel *model = self.tableViewArray[indexPath.section];
    [cell setCellModel:model];
    return cell;
}

#pragma mark - 前往商品详情页
- (void)gotoGoodsDelitgClick:(UIButton *)sender
{
    NSLog(@"前往商品详情页");
}

#pragma mark - 改价btn绑定事件
- (void)changePriceBtnClick:(UIButton *)sender
{
    MainModel *model = self.tableViewArray[sender.indexPath.section];
    MYHAleartView *alertView = [[MYHAleartView alloc] initChangPriceWithModel:model];
    [alertView showAlertView];
    __weak ChangePriceController *myself = self;
    alertView.moneyBlock = ^(CGFloat number)
    {
        NSLog(@"%0.2f",number);
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        [dic setValue:model.product_id forKey:@"itemId"];
        [dic setValue:[NSString stringWithFormat:@"%0.2f",number] forKey:@"price"];
        
        [HttpRequest formPostPath:EditGoods_API params:dic resultBlock:^(id Data, MsgModel *msgModel)
        {
            NSLog(@"改价Data--%@",Data);
            [myself httpRequestNewData];
        }];
    };
}

#pragma mark - 分享商品点击事件
- (void)sharkShoppingClick:(UIButton *)sender
{
    NSLog(@"一键分享");
    MainModel *model = self.tableViewArray[sender.indexPath.section];
    SharkShoppingVC *shopping = [[SharkShoppingVC alloc] init];
    shopping.product_id = model.product_id;
    shopping.hidesBottomBarWhenPushed = YES;
    shopping.isHide = NO;
    [self.navigationController pushViewController:shopping animated:YES];
}


@end
